using System;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;

namespace OurWedding1.Models
{ 
    public class MailingConfigurationRepository : IMailingConfigurationRepository
    {
        readonly WeddingEntities _context = new WeddingEntities();

        public IQueryable<MailingConfiguration> All
        {
            get { return _context.MailingConfigurations; }
        }

        public IQueryable<MailingConfiguration> AllIncluding(params Expression<Func<MailingConfiguration, object>>[] includeProperties)
        {
            IQueryable<MailingConfiguration> query = _context.MailingConfigurations;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public MailingConfiguration Find(int id)
        {
            return _context.MailingConfigurations.Find(id);
        }

        public void InsertOrUpdate(MailingConfiguration mailingconfiguration)
        {
            if (mailingconfiguration.MailingConfigurationId == default(int)) {
                // New entity
                _context.MailingConfigurations.Add(mailingconfiguration);
            } else {
                // Existing entity
                _context.Entry(mailingconfiguration).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var mailingconfiguration = _context.MailingConfigurations.Find(id);
            _context.MailingConfigurations.Remove(mailingconfiguration);
        }

        public void Save()
        {
            _context.SaveChanges();
        }

        public void Dispose() 
        {
            _context.Dispose();
        }
    }

    public interface IMailingConfigurationRepository : IDisposable
    {
        IQueryable<MailingConfiguration> All { get; }
        IQueryable<MailingConfiguration> AllIncluding(params Expression<Func<MailingConfiguration, object>>[] includeProperties);
        MailingConfiguration Find(int id);
        void InsertOrUpdate(MailingConfiguration mailingconfiguration);
        void Delete(int id);
        void Save();
    }
}